//
//  RegisterViewController.m
//  DeliciousFood1
//
//  Created by brother on 15/10/21.
//  Copyright © 2015年 com.hxm. All rights reserved.
//

#import "RegisterViewController.h"
#import <AFNetworking.h>

@interface RegisterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *iphone;
@property (weak, nonatomic) IBOutlet UITextField *verityCode;
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UIButton *man;
@property (weak, nonatomic) IBOutlet UIButton *woman;
@property (weak, nonatomic) IBOutlet UITextField *password1;
@property (weak, nonatomic) IBOutlet UITextField *password2;

//发送验证码
- (IBAction)sendCode:(id)sender;
//注册
- (IBAction)myregister:(id)sender;

@end

@implementation RegisterViewController

- (IBAction)sendCode:(id)sender {
}

- (IBAction)myregister:(id)sender {
    //判断信息是否填写完整！
    if ([self.iphone.text isEqualToString:@""]||[self.verityCode.text isEqualToString:@""]||[self.name.text isEqualToString:@""]||[self.password1.text isEqualToString:@""]||[self.password2.text isEqualToString:@""]||(!self.man.selected&&!self.woman.selected)) {
        [[[UIAlertView alloc] initWithTitle:@"提示" message:@"用户信息填写不完善！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
    }else
    {
        //判断两次密码是否一致
        if ([self.password1.text isEqualToString:self.password2.text]) {
            //验证码判定
                //注册信息
                AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
                
                NSString *url = @"http://localhost:8888/thinkphp/index.php/home/info/registerUser";
                
                NSMutableDictionary *dic = [NSMutableDictionary dictionary];
                dic[@"account"] = self.iphone.text;
                dic[@"password"] = self.password1.text;
                dic[@"username"] = self.name.text;
                int a = self.man.selected == YES ? 0:1;
                dic[@"sex"] = [NSNumber numberWithInt:a];
            
                [manager GET:url parameters:dic success:^void(AFHTTPRequestOperation * operation, id  responseObject) {
                    if ([responseObject[@"code"] intValue] == 0) {
                        [[[UIAlertView alloc] initWithTitle:@"提示" message:@"注册成功，现在去登陆！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
                    }else
                        [[[UIAlertView alloc] initWithTitle:@"提示" message:@"啊偶，注册失败啦！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
                } failure:^(AFHTTPRequestOperation *  operation, NSError * error) {
                     [[[UIAlertView alloc] initWithTitle:@"提示" message:@"啊偶，注册失败啦！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
                }];
        }else
        {
            [[[UIAlertView alloc] initWithTitle:@"提示" message:@"两次密码填写不一致！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
        }
    }
}
//改变按钮
- (IBAction)change:(UIButton *)sender {
    if (self.man.selected == NO) {
        [self.man setBackgroundImage:[UIImage imageNamed:@"YES"] forState:UIControlStateNormal];
        [self.woman setBackgroundImage:[UIImage imageNamed:@"NO"] forState:UIControlStateNormal];
        self.man.selected = YES;
        self.woman.selected = NO;
    } else {
        [self.woman setBackgroundImage:[UIImage imageNamed:@"YES"] forState:UIControlStateNormal];
        [self.man setBackgroundImage:[UIImage imageNamed:@"NO"] forState:UIControlStateNormal];
        self.woman.selected = YES;
        self.man.selected = NO;
    }
    
}


@end
